package com.scd.jmeter.rmi.common;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;

public class RmiUtils {

    public static final String JMETER_ENGINE_RMI_NAME = "JMeterEngine";

    public static final boolean SSL_DISABLED = true;

    public static RMIClientSocketFactory createClientSocketFactory() {
//        if (SSL_DISABLED) {
//            log.info("Disabling SSL for RMI as server.rmi.ssl.disable is set to 'true'");
//            return null;
//        }
//        if (StringUtils.isBlank(KEYSTORE_FILE)) {
//            Validate.validState(SSL_DISABLED,
//                    "No keystore for RMI over SSL specified. Set 'server.rmi.ssl.disable' to true, if this is intentional,"
//                            + "if not run create-rmi-keystore.bat/create-rmi-keystore.sh to create a keystore and distribute it on client and servers"
//                            + "used for distributed testing.");
//            return null;
//        }
//        final SSLRMIClientSocketFactory factory = new SSLRMIClientSocketFactory();
//        factory.setAlias(KEYSTORE_ALIAS);
//        factory.setKeystore(KEYSTORE_FILE, KEYSTORE_TYPE, KEYSTORE_PASSWORD);
//        factory.setTruststore(TRUSTSTORE_FILE, TRUSTSTORE_TYPE, TRUSTSTORE_PASSWORD);
//        return factory;
        return null;
    }

    public static RMIServerSocketFactory createServerSocketFactory() throws RemoteException {
//        if (SSL_DISABLED) {
//            log.info("Disabling SSL for RMI as server.rmi.ssl.disable is set to 'true'");
//            return null;
//        }
//        if (StringUtils.isBlank(KEYSTORE_FILE)) {
//            Validate.validState(SSL_DISABLED,
//                    "No keystore for RMI over SSL specified. Set 'server.rmi.ssl.disable' to true, if this is intentional.");
//            return new RMIServerSocketFactoryImpl(getRmiHost());
//        }
//        SSLRMIServerSocketFactory factory = new SSLRMIServerSocketFactory(getRmiHost());
//        factory.setAlias(KEYSTORE_ALIAS);
//        factory.setNeedClientAuth(true);
//        factory.setKeystore(KEYSTORE_FILE, KEYSTORE_TYPE, KEYSTORE_PASSWORD);
//        factory.setTruststore(TRUSTSTORE_FILE, TRUSTSTORE_TYPE, TRUSTSTORE_PASSWORD);
//        return factory;
        return null;
    }

    /**
     * @return the configure address for usage by RMI
     * @throws RemoteException when no valid address can be found
     */
    public static InetAddress getRmiHost() throws RemoteException {
        InetAddress localHost=null;
        // Bug 47980 - allow override of local hostname
        String host = System.getProperties().getProperty("java.rmi.server.hostname"); // $NON-NLS-1$
        try {
            if( host==null ) {
//                log.info("System property 'java.rmi.server.hostname' is not defined, using localHost address");
                localHost = InetAddress.getLocalHost();
            } else {
//                log.info("Resolving by name the value of System property 'java.rmi.server.hostname': {}", host);
                localHost = InetAddress.getByName(host);
            }
        } catch (UnknownHostException e) {
            throw new RemoteException("Cannot start. Unable to get local host IP address.", e);
        }
//        if (log.isInfoEnabled()) {
//            log.info("Local IP address={}", localHost.getHostAddress());
//        }
        // BUG 52469 : Allow loopback address for SSH Tunneling of RMI traffic
        if (host == null && localHost.isLoopbackAddress()){
            String hostName = localHost.getHostName();
            throw new RemoteException("Cannot start. " + hostName + " is a loopback address.");
        }
        return localHost;
    }

}
